import MySQLdb
import threading

class Connection:
    def __init__(self):
        self.Connect()
        self.Lock = threading.Lock()
    
    def Connect(self):
        print "Connecting"
        self.conn = MySQLdb.connect (host = "SQL09.FREEMYSQL.NET",
                            user = "jamesdcollett",
                             passwd = "potato99",
                             db = "costcalc")
        self.cursor = self.conn.cursor ()
        
    def close(self):
        self.conn.close()
        
    def SendCommand(self, Command):
        self.Lock.acquire(10)
        try:
            res = self.cursor.execute(Command)
        except:
            self.Connect()
            res = self.cursor.execute(Command)
        if "SELECT" not in Command:
            self.Lock.release()
        return res
        
            
    def FetchAll(self):
        Res = self.cursor.fetchall()
        self.Lock.release()
        return Res
        